package Service;

import  java.io.*;
import java.util.ArrayList;
import java.util.List;

import Pojo.Book;


public class LibraryService {
    public List<Book>findBookAll(String bookId, String bookName, String bookType, String loanReaderId){
        List<Book>list = new ArrayList<>();
        try{
            BufferedReader br = new BufferedReader(new FileReader("src\\image\\Book.txt"));
            String line = null;
            while ((line=br.readLine())!=null){
                Book book = new Book();
                String[] array = line.split(",");
                book.setBookId(array[0]);
                book.setBookName(array[1]);
                book.setBookType(array[2]);
                book.setLoanReaderId(array[3]);
                list.add(book);
            }
        }catch (FileNotFoundException e){
            throw new RuntimeException(e);
        }catch(IOException e){
            throw new RuntimeException(e);
        }
        if(bookId==null || bookName==null || bookType==null){
            return list;
        }else if(bookId!=null){
            List<Book>Idlist = new ArrayList<>();
            for(int i=0;i<list.size();i++){
                if(list.get(i).getBookId().equals(Idlist)){
                    Idlist.add(list.get(i));
                }
            }
            return Idlist;
        }
        return list;
    }
    public String[][]listToArray(List<Book>list){
        String[][]obj = new String[list.size()][3];
        for(int i= 0;i<list.size();i++){
            Book book = list.get(i);
            obj[i] = new String[]
                    {
                            book.getBookId(),book.getBookName(),book.getBookType(),book.getLoanReaderId()
                    };
        }
        return obj;
    }
    public void delBook(int index) {
        List<Book> list = findBookAll(null,null,null,null);
        list.remove(index);
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\Demo123\\src\\image\\Book.txt"));
            for (int i = 0; i < list.size(); i++) {
                String line = list.get(i).getBookId() + "," + list.get(i).getBookName() + "," + list.get(i).getBookType() + "," + list.get(i).getLoanReaderId();
                bw.write(line);
                if (i != list.size() - 1) {
                    bw.newLine();
                }
            }
            bw.flush();
        }catch (IOException e){
            throw new RuntimeException(e);
        }

    }
    public Book findBookById(int index){
        List<Book>list = findBookAll(null,null,null,null);
        return list.get(index);
    }
    public void editBook(Book book){
        List<Book> list = findBookAll(null,null,null,null);
        for(int i=0;i<list.size();i++){
            if(list.get(i).getBookId().equals(book.getBookId())){
                list.set(i,book);
            }
        }
        try{
            BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\Demo123\\src\\image\\Book.txt"));
            for (int i=0;i<list.size();i++){
                Book book1 = list.get(i);
                String line = book1.getBookId()+","+book1.getBookName()+","+book1.getBookType()+","+book1.getLoanReaderId();
                bw.write(line);
                if(i<list.size() -1){
                    bw.newLine();
                }
            }
            bw.flush();

        }catch (IOException e){
            throw new RuntimeException(e);
        }
    }
    public static void saveBook(String bookId, String bookName, String bookType, String loanReaderId){
        try {
            BufferedWriter bw = new BufferedWriter(new
                    FileWriter("D:\\Demo123\\src\\image\\Book.txt",true));
            String line = bookId+","+bookName +","+bookType+","+ loanReaderId;
            bw.newLine();
            bw.write(line);
            bw.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

